package com.sgb.packers.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sgb.packers.R;
import com.swgk.core.dialog.BaseBottomDialog;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.DialogViewHolder;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.interfaces.DialogClick;
import com.swgk.core.util.MToast;
import com.swgk.core.util.NewDialogHelper;
import com.zjkj.qgbui.widgets.inputtext.DecimalInputTextWatcher;

import java.util.Locale;

/**
 * @author : younghow
 * @date : 2021/11/24 16:45
 * description:
 */
public class SalaryPopWindow extends BaseBottomDialog implements View.OnClickListener {

    private ImageView mIvAddWorkForHour;
    private ImageView mIvAddWorkForDay;
    private Sure mSure;
    private double mWorkNumber = 0;
    private String workOvertimeType = "2";
    private EditText mTvSelectSalaryNumber;
    private EditText mEtInputAddWorkSalary;
    private TextView mEtInputAddWorkSalaryForDay;
    private TextView mTvInputAddWorkSalaryForDay;
    private TextView mTvInputAddWorkSalary;
    private TextView tvSelectUpWorkNumber;
    private TextView tvSalaryResult;


    public SalaryPopWindow(Context context, DialogCallBack dialogCallBack) {
        super(context, dialogCallBack);
    }

    public void setSure(Sure mSure) {
        this.mSure = mSure;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_select_up_work_number) {
            dismiss();
            TimeSalaryPopWindow timeSalaryPopWindow = new TimeSalaryPopWindow(context);
            timeSalaryPopWindow.setDialogClick(new DialogClick<String>() {
                @Override
                public void confirmClick(String s, Dialog dialog) {
                    if ("".equals(s)) {
                        MToast.showToast(context, "请选择上班时长");
                        return;
                    }
                    tvSelectUpWorkNumber.setText(s.replace("小时",""));
                    mWorkNumber = Double.parseDouble(s.replace("小时", ""));
                    show();
                    timeSalaryPopWindow.dismiss();
                }

                @Override
                public void cancelClick() {
                    show();
                    timeSalaryPopWindow.dismiss();
                }
            });
            timeSalaryPopWindow.showPopwindow(tvSelectUpWorkNumber, Gravity.BOTTOM, 0, 0);
        } else if (v.getId() == R.id.iv_add_work_for_hour) {
            mEtInputAddWorkSalaryForDay.setText("");
            if (!mIvAddWorkForHour.isSelected()) {
                mIvAddWorkForHour.setSelected(true);
                mIvAddWorkForDay.setSelected(false);
                workOvertimeType = "2";
                mEtInputAddWorkSalary.setVisibility(View.VISIBLE);
                mTvInputAddWorkSalary.setVisibility(View.VISIBLE);
                mEtInputAddWorkSalaryForDay.setVisibility(View.INVISIBLE);
                mTvInputAddWorkSalaryForDay.setVisibility(View.INVISIBLE);
                tvSalaryResult.setText(String.format(Locale.CHINA, "加班每小时多少钱：%.2f元/小时", 0.000));
            }
        } else if (v.getId() == R.id.tv_add_work_for_hour) {
            mEtInputAddWorkSalaryForDay.setText("");
            if (!mIvAddWorkForHour.isSelected()) {
                mIvAddWorkForHour.setSelected(true);
                mIvAddWorkForDay.setSelected(false);
                workOvertimeType = "2";
                mEtInputAddWorkSalary.setVisibility(View.VISIBLE);
                mTvInputAddWorkSalary.setVisibility(View.VISIBLE);
                mEtInputAddWorkSalaryForDay.setVisibility(View.INVISIBLE);
                mTvInputAddWorkSalaryForDay.setVisibility(View.INVISIBLE);
                tvSalaryResult.setText(String.format(Locale.CHINA, "加班每小时多少钱：%.2f元/小时", 0.000));
            }
        } else if (v.getId() == R.id.iv_add_work_for_day) {
            mEtInputAddWorkSalary.setText("");
            if (!mIvAddWorkForDay.isSelected()) {
                mIvAddWorkForDay.setSelected(true);
                mIvAddWorkForHour.setSelected(false);
                workOvertimeType = "1";
                mEtInputAddWorkSalary.setVisibility(View.INVISIBLE);
                mTvInputAddWorkSalary.setVisibility(View.INVISIBLE);
                mEtInputAddWorkSalaryForDay.setVisibility(View.VISIBLE);
                mTvInputAddWorkSalaryForDay.setVisibility(View.VISIBLE);
                tvSalaryResult.setText(String.format(Locale.CHINA, "加班每小时多少钱：%.2f元/小时", 0.000));
            }
        } else if (v.getId() == R.id.tv_add_work_for_day) {
            mEtInputAddWorkSalary.setText("");
            if (!mIvAddWorkForDay.isSelected()) {
                mIvAddWorkForDay.setSelected(true);
                mIvAddWorkForHour.setSelected(false);
                workOvertimeType = "1";
                mEtInputAddWorkSalary.setVisibility(View.INVISIBLE);
                mTvInputAddWorkSalary.setVisibility(View.INVISIBLE);
                mEtInputAddWorkSalaryForDay.setVisibility(View.VISIBLE);
                mTvInputAddWorkSalaryForDay.setVisibility(View.VISIBLE);
                tvSalaryResult.setText(String.format(Locale.CHINA, "加班每小时多少钱：%.2f元/小时", 0.000));
            }
        } else if (v.getId() == R.id.tv_pop_sure) {
            if ((TextUtils.isEmpty(mEtInputAddWorkSalary.getText().toString()) && TextUtils.isEmpty(mEtInputAddWorkSalaryForDay.getText().toString()))
                    || TextUtils.isEmpty(mTvSelectSalaryNumber.getText().toString()) || TextUtils.isEmpty(tvSelectUpWorkNumber.getText().toString())) {
                RoundCornerDialog cornerDialog = new NewDialogHelper<String>(context, R.layout.library_dialog_salary_sure)
                        .text(R.id.tv_title, "温馨提示")
                        .visible(R.id.tv_title, View.VISIBLE)
                        .text(R.id.tv_message, "工资标准没有配置完，当日工资无法计算，是否继续配置工资标准？")
                        .text(R.id.tv_logout_cancel, "取消")
                        .text(R.id.tv_logout_confirm, "确定")
                        .onClick(R.id.tv_logout_confirm, new DialogClick<String>() {
                            @Override
                            public void confirmClick(String s, Dialog dialog) {
                                dialog.dismiss();
                            }
                        })
                        .onClick(R.id.tv_logout_cancel, new DialogClick<String>() {
                            @Override
                            public void confirmClick(String s, Dialog dialog) {
                                dialog.dismiss();
                                dismiss();
                            }
                        })
                        .build();
                cornerDialog.show();
                return;
            }


            if (!"".equals(mEtInputAddWorkSalary.getText().toString())) {
                mSure.sure(workOvertimeType, mWorkNumber, Double.parseDouble(mTvSelectSalaryNumber.getText().toString()), Double.parseDouble(mEtInputAddWorkSalary.getText().toString()));
                return;
            }

            if (!"".equals(mEtInputAddWorkSalaryForDay.getText().toString())) {
                mSure.sure(workOvertimeType, mWorkNumber, Double.parseDouble(mTvSelectSalaryNumber.getText().toString()), Double.parseDouble(mEtInputAddWorkSalaryForDay.getText().toString()));
                return;
            }

            if (!"".equals(mTvSelectSalaryNumber.getText().toString())) {
                mSure.sure(workOvertimeType, mWorkNumber, Integer.parseInt(mTvSelectSalaryNumber.getText().toString()));
            } else {
                if (mWorkNumber == 0) {
                    mSure.sure(workOvertimeType, 0.0);
                } else {
                    mSure.sure(workOvertimeType, mWorkNumber);
                }
            }
        } else if (v.getId() == R.id.tv_pop_cancel) {
            dismiss();
        } else if (v.getId() == R.id.et_input_add_work_salary_for_day) {
            mEtInputAddWorkSalary.setText("");
            dismiss();
            TimeSalaryPopWindow timeSalaryPopWindow = new TimeSalaryPopWindow(context);
            timeSalaryPopWindow.setDialogClick(new DialogClick<String>() {
                @Override
                public void confirmClick(String s, Dialog dialog) {
                    if ("".equals(s)) {
                        MToast.showToast(getContext(), "请选择加班时长");
                        return;
                    }

                    if (!TextUtils.isEmpty(mTvSelectSalaryNumber.getText().toString())) {
                        tvSalaryResult.setText(String.format(Locale.CHINA, "加班每小时多少钱：%.2f元/小时", Double.parseDouble(mTvSelectSalaryNumber.getText().toString()) / Double.parseDouble(s.replace("小时", ""))));
                    }
                    mEtInputAddWorkSalaryForDay.setText(s.replace("小时", ""));
                    timeSalaryPopWindow.dismiss();
                    show();
                }

                @Override
                public void cancelClick() {
                    show();
                }
            });
            timeSalaryPopWindow.showPopwindow(tvSelectUpWorkNumber, Gravity.BOTTOM, 0, 0);
        }
    }

    public void setDefault(String type, double... defaultSalary) {
        if (defaultSalary.length == 0) {
            return;
        }

        if (defaultSalary.length == 1) {
            tvSelectUpWorkNumber.setText(String.format(Locale.CHINA, "%.2f", defaultSalary[0]));
        }

        if (defaultSalary.length == 2) {
            tvSelectUpWorkNumber.setText(String.format(Locale.CHINA, "%.2f", defaultSalary[0]));
            mTvSelectSalaryNumber.setText(String.format(Locale.CHINA, "%.2f", defaultSalary[1]));
        }

        if (defaultSalary.length == 3) {
            tvSelectUpWorkNumber.setText(String.format(Locale.CHINA, "%.2f", defaultSalary[0]));
            mTvSelectSalaryNumber.setText(String.format(Locale.CHINA, "%.2f", defaultSalary[1]));
            if ("2".equals(type)) {
                mEtInputAddWorkSalary.setVisibility(View.VISIBLE);
                mEtInputAddWorkSalaryForDay.setVisibility(View.INVISIBLE);
                mTvInputAddWorkSalaryForDay.setVisibility(View.VISIBLE);
                mTvInputAddWorkSalary.setVisibility(View.VISIBLE);
                mTvInputAddWorkSalaryForDay.setVisibility(View.INVISIBLE);
                mIvAddWorkForHour.setSelected(true);
                mIvAddWorkForDay.setSelected(false);
                mEtInputAddWorkSalary.setText(String.format(Locale.CHINA, "%.2f", defaultSalary[2]));
            } else {
                mEtInputAddWorkSalary.setVisibility(View.INVISIBLE);
                mEtInputAddWorkSalaryForDay.setVisibility(View.VISIBLE);
                mTvInputAddWorkSalary.setVisibility(View.INVISIBLE);
                mTvInputAddWorkSalaryForDay.setVisibility(View.VISIBLE);
                mIvAddWorkForHour.setSelected(false);
                mIvAddWorkForDay.setSelected(true);
                mEtInputAddWorkSalaryForDay.setText(String.format(Locale.CHINA, "%.2f", defaultSalary[2]));
            }
        }

    }

    public void clear() {
        mEtInputAddWorkSalaryForDay.setText("");
        mEtInputAddWorkSalary.setText("");
        mTvSelectSalaryNumber.setText("");
        tvSelectUpWorkNumber.setText("");
        mWorkNumber = 0;
        tvSalaryResult.setText(String.format(Locale.CHINA, "加班每小时多少钱：%.2f元/小时", 0.000));
        workOvertimeType = "2";
        mIvAddWorkForHour.setSelected(true);
        mIvAddWorkForDay.setSelected(false);
    }

    @Override
    protected int dialogLayout() {
        return R.layout.packers_pop_salary;
    }

    @Override
    protected void initView(DialogViewHolder holder, Bundle savedInstanceState) {
        tvSelectUpWorkNumber = holder.getDialog().findViewById(R.id.tv_select_up_work_number);
        mTvSelectSalaryNumber = holder.getDialog().findViewById(R.id.tv_select_salary_number);
        mIvAddWorkForHour = holder.getDialog().findViewById(R.id.iv_add_work_for_hour);
        TextView mTvAddWorkForHour = holder.getDialog().findViewById(R.id.tv_add_work_for_hour);
        mIvAddWorkForDay = holder.getDialog().findViewById(R.id.iv_add_work_for_day);
        TextView mTvAddWorkForDay = holder.getDialog().findViewById(R.id.tv_add_work_for_day);
        TextView tvPopSure = holder.getDialog().findViewById(R.id.tv_pop_sure);
        TextView tvPopCancel = holder.getDialog().findViewById(R.id.tv_pop_cancel);
        mEtInputAddWorkSalary = holder.getDialog().findViewById(R.id.et_input_add_work_salary);
        mEtInputAddWorkSalaryForDay = holder.getDialog().findViewById(R.id.et_input_add_work_salary_for_day);
        mTvInputAddWorkSalaryForDay = holder.getDialog().findViewById(R.id.tv_input_add_work_salary_for_day);
        mTvInputAddWorkSalary = holder.getDialog().findViewById(R.id.tv_input_add_work_salary);
        tvSalaryResult = holder.getDialog().findViewById(R.id.tv_salary_result);

        mIvAddWorkForHour.setSelected(true);

        tvSelectUpWorkNumber.setOnClickListener(this);
        mIvAddWorkForHour.setOnClickListener(this);
        mTvAddWorkForHour.setOnClickListener(this);
        mIvAddWorkForDay.setOnClickListener(this);
        mTvAddWorkForDay.setOnClickListener(this);
        tvPopSure.setOnClickListener(this);
        tvPopCancel.setOnClickListener(this);
        mEtInputAddWorkSalaryForDay.setOnClickListener(this);

        mTvSelectSalaryNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String s = editable.toString();
                if (s.contains(".")) {
                    mTvSelectSalaryNumber.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6 + 2 + 1)});
                    if (s.length() - 1 - s.indexOf(".") > 2) {
                        s = s.substring(0, s.indexOf(".") + 2 + 1);
                        editable.replace(0, editable.length(), s.trim());
                    }
                } else {
                    mTvSelectSalaryNumber.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6 + 1)});
                    if (s.length() > 6) {
                        s = s.substring(0, 6);
                        editable.replace(0, editable.length(), s.trim());
                    }

                }
                if (".".equals(s.trim())) {
                    s = "0" + s;
                    editable.replace(0, editable.length(), s.trim());
                }

                if (s.startsWith("0") && s.trim().length() > 1) {
                    if (s.charAt(1) != '.') {
                        editable.replace(0, editable.length(), "0");
                    }
                }

                if (!TextUtils.isEmpty(mEtInputAddWorkSalaryForDay.getText().toString()) && !TextUtils.isEmpty(mTvSelectSalaryNumber.getText().toString())) {
                    tvSalaryResult.setText(String.format(Locale.CHINA, "加班每小时多少钱：%.2f元/小时", Double.parseDouble(mTvSelectSalaryNumber.getText().toString()) / Double.parseDouble(mEtInputAddWorkSalaryForDay.getText().toString())));
                }else {
                    tvSalaryResult.setText(String.format(Locale.CHINA, "加班每小时多少钱：%.2f元/小时",0.00));
                }
            }
        });
        mEtInputAddWorkSalary.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String s = editable.toString();
                if (s.contains(".")) {
                    mEtInputAddWorkSalary.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6 + 2 + 1)});
                    if (s.length() - 1 - s.indexOf(".") > 2) {
                        s = s.substring(0, s.indexOf(".") + 2 + 1);
                        editable.replace(0, editable.length(), s.trim());
                    }
                } else {
                    mEtInputAddWorkSalary.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6 + 1)});
                    if (s.length() > 6) {
                        s = s.substring(0, 6);
                        editable.replace(0, editable.length(), s.trim());
                    }

                }
                if (".".equals(s.trim())) {
                    s = "0" + s;
                    editable.replace(0, editable.length(), s.trim());
                }

                if (s.startsWith("0") && s.trim().length() > 1) {
                    if (s.charAt(1) != '.') {
                        editable.replace(0, editable.length(), "0");
                    }
                }

                if (!TextUtils.isEmpty(mEtInputAddWorkSalary.getText().toString())){
                    tvSalaryResult.setText(String.format(Locale.CHINA, "加班每小时多少钱：%.2f元/小时", Double.valueOf(mEtInputAddWorkSalary.getText().toString())));
                }
            }
        });
    }

    public interface Sure {
        void sure(String type, double hour, double... addWorkSalary);
    }
}
